package com.woniu.liuzhougov.WorkPlan.controller;

import com.woniu.liuzhougov.WorkPlan.Enum.PlanEnum;
import com.woniu.liuzhougov.WorkPlan.Enum.PlanLog;
import com.woniu.liuzhougov.WorkPlan.Enum.PlanServiceEnum;
import com.woniu.liuzhougov.WorkPlan.Exception.PlanException;
import com.woniu.liuzhougov.WorkPlan.dto.PlanDTO;
import com.woniu.liuzhougov.WorkPlan.dto.PlanDepartmentDTO;
import com.woniu.liuzhougov.WorkPlan.pojo.Plan;
import com.woniu.liuzhougov.WorkPlan.service.PlanService;
import com.woniu.liuzhougov.WorkPlan.util.IJSONResult;
import com.woniu.liuzhougov.authorityControl.constant.UserConstant;
import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.pojo.Department;
import com.woniu.liuzhougov.itemAndTask.pojo.ItemsPojo;
import com.woniu.liuzhougov.itemAndTask.service.DepartmentService;
import com.woniu.liuzhougov.systemSettings.pojo.Log;
import com.woniu.liuzhougov.systemSettings.service.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 工作计划
 * @Author: junzhao
 * @Date: 2019-04-19 16:08
 */
@RestController
@RequestMapping(value = "plan")
public class PlanController {

    @Autowired
    private PlanService planService;

    @Resource
    private DepartmentService departmentService;

    private String resultMessage;

    @Autowired
    private LogService logService;

    /**
     *
     * @param plan
     * @param httpSession
     * @return
     */
    @RequiresPermissions(value = "plan_add")
    @PostMapping(value = "/add")
    public IJSONResult insertPlan(Plan plan, HttpSession httpSession) {
        //获取当前用户的ID值
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        plan.setPlanFounderId(user.getUserId());
        //执行添加
        resultMessage = planService.insertSelective(plan);
        logService.insert(new Log(0, PlanLog.add.getType(),user.getUserId()));
        return IJSONResult.ok(PlanServiceEnum.OK.getMessage());
    }

    /**
     * 查询工作计划详情
     *
     * @param planId
     * @return
     */
    @RequiresPermissions(value = "plan_detail")
    @GetMapping(value = "/details")
    public IJSONResult selectByPrimaryKeyAndUserId(@RequestParam("planId") Integer planId) {
        PlanDTO planDTO = planService.selectByPrimaryKeyAndUserId(planId);
        return IJSONResult.ok(planDTO);
    }

    /**
     * 动态分页查询工作计划表 按照年度查 按月度查
     *
     * @param page
     * @param planDTO
     * @return
     */
    @GetMapping(value = "/list")
    public IJSONResult selectPlanByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        PlanDTO planDTO,HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        Department department = user.getDepartment();
        planDTO.setPlanDepartmentId(department.getDepartmentId());
        Integer pageSize = 10;
        List<PlanDTO> plan = planService.queryPlanByPage(planDTO, page, pageSize);
        return IJSONResult.ok(plan);
    }

    /**
     * 更新工作计划内容
     *
     * @param planDTO
     * @return
     */
    @RequiresPermissions(value = "plan_update")
    @PostMapping(value = "/update")
    public IJSONResult updateByPrimaryKeySelective(PlanDTO planDTO,HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        resultMessage = planService.updateByPrimaryKeySelective(planDTO);
        logService.insert(new Log(0, PlanLog.update.getType(),user.getUserId()));
        return IJSONResult.ok(resultMessage);
    }

    /**
     * 删除工作计划
     *
     * @param planId
     * @return
     */
    @RequiresPermissions(value = "plan_delete")
    @PostMapping(value = "/delete")
    public IJSONResult deleteByPrimaryKey(@RequestParam("planId") Integer planId,HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        resultMessage = planService.deleteByPrimaryKey(planId);
        logService.insert(new Log(0, PlanLog.delete.getType(),user.getUserId()));
        return IJSONResult.ok(resultMessage);
    }

    /**
     * @Description: 查询当前登陆用户所在的部门的部门
     * @Author: JunZhao
     * @Date: 2019-04-22 18:02
     * @Param: []
     * @Return: com.woniu.liuzhougov.WorkPlan.util.IJSONResult
     */

    @GetMapping(value = "/department/list")
    public IJSONResult queryAllItem(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        Department department = user.getDepartment();


        List<PlanDepartmentDTO> itemDTOList = new ArrayList<>();
        List<Department> itemList = departmentService.listDepartments();
        for (Department i : itemList) {
            if(department.getDepartmentId()==i.getDepartmentId()){
                PlanDepartmentDTO planItemDTO = new PlanDepartmentDTO();
                BeanUtils.copyProperties(i,planItemDTO);
                itemDTOList.add(planItemDTO);
            }
        }
        return IJSONResult.ok(itemDTOList);
    }

    /**
     * 发布工作计划到官网
     *
     * @param planDTO
     * @return
     */
    @RequiresPermissions(value = "plan_release")
    @PostMapping(value = "/release")
    public IJSONResult planRelease(PlanDTO planDTO,HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        logService.insert(new Log(0, PlanLog.release.getType(),user.getUserId()));
        try {
            resultMessage = planService.planRelease(planDTO);
        } catch (PlanException p) {
            return IJSONResult.errorMsg(PlanServiceEnum.FAIL.getMessage());
        }
        return IJSONResult.ok(resultMessage);
    }
    /**
     * @Description:    个人关联的部门的工作计划动态查询
     * @Author: JunZhao @RequestParam(value = "page", defaultValue = "1") Integer page,
     *                                         PlanDTO planDTO
     * @Date: 2019-04-28 09:53
     * @Param: []
     * @Return: com.woniu.liuzhougov.WorkPlan.util.IJSONResult
     */
    @GetMapping(value = "/personal/list")
    public IJSONResult personalPlan(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                    PlanDTO planDTO,
                                    HttpSession httpSession){
        User user = (User) httpSession.getAttribute(UserConstant.CURRENT_USER);
        planDTO.setPlanRelease(PlanEnum.PLAN_RELEASE.getCode());
        planDTO.setPlanDepartmentId(user.getDepartment().getDepartmentId());
        Integer pageSize = 10;
        //拿到当前用户
        List<PlanDTO> plan = planService.queryPlanByPage(planDTO, page, pageSize);
        //查询
        return IJSONResult.ok(plan);
    }
}
